export const getLocalTime = function(timestamp) {
  if (timestamp) {
    var date = new Date(timestamp.time);
    var Y = date.getFullYear() + "-";
    var M =
      (date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1)
        : date.getMonth() + 1) + "-";
    var D = date.getDate();
    return Y + M + D;
  }
};

export const delayed = function(time) {
  return new Promise(r => {
    setTimeout(() => {
      r(true);
    }, time);
  });
};

export const binaryInsertionSort = function(array) {
  for (var i = 1; i < array.length; i++) {
    var key = array[i],
      left = 0,
      right = i - 1;
    while (left <= right) {
      var middle = parseInt((left + right) / 2);
      if (key < array[middle]) {
        right = middle - 1;
      } else {
        left = middle + 1;
      }
    }
    for (var j = i - 1; j >= left; j--) {
      array[j + 1] = array[j];
    }
    array[left] = key;
  }
  return array;
};

export const videoDimensions = function(video) {
  // Ratio of the video's intrisic dimensions
  var videoRatio = video.videoWidth / video.videoHeight;
  // The width and height of the video element
  var width = video.offsetWidth,
    height = video.offsetHeight;
  // The ratio of the element's width to its height
  var elementRatio = width / height;
  // If the video element is short and wide
  if (elementRatio > videoRatio) width = height * videoRatio;
  // It must be tall and thin, or exactly equal to the original ratio
  else height = width / videoRatio;
  return {
    width: width,
    height: height
  };
};

export const formatSeconds = function(value) {
  var secondTime = parseInt(value); // 秒
  var minuteTime = 0; // 分
  // var hourTime = 0; // 小时
  if (secondTime > 60) {
    //如果秒数大于60，将秒数转换成整数
    //获取分钟，除以60取整数，得到整数分钟
    minuteTime = parseInt(secondTime / 60);
    //获取秒数，秒数取佘，得到整数秒数
    secondTime = parseInt(secondTime % 60);
    //如果分钟大于60，将分钟转换成小时
    if (minuteTime > 60) {
      //获取小时，获取分钟除以60，得到整数小时
      // hourTime = parseInt(minuteTime / 60);
      //获取小时后取佘的分，获取分钟除以60取佘的分
      minuteTime = parseInt(minuteTime % 60);
    }
  }
  let 秒 = 0;
  let 分 = 0;
  秒 = "" + parseInt(secondTime);
  if (minuteTime > 0) {
    分 = "" + parseInt(minuteTime);
  }
  // if (hourTime > 0) {
  //   时 = parseInt(hourTime);
  // }
  return `${分}:${秒}`;
};
